//
//  BusRouteDataSource.m
//  Autobusim
//
//  Created by Hezi Cohen on 11/27/08.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import "BusRouteDataSource.h"

enum Sections {
	kToSection = 0,
	kFromSection,
	NUM_OF_SECTIONS
};

@implementation BusRouteDataSource
@synthesize fromPlace;
@synthesize toPlace;
@synthesize fromHours;
@synthesize toHours;

- (void)setDelegate:(id)aDelegate
{
	delegate = aDelegate;
}

- (id)initFromPlace:(NSString*)from toPlace:(NSString*)to withHoursFrom:(NSString*)fromH andHoursTo:(NSString*)toH {
	self = [super init];
	
	if(self) {
		self.toPlace = to;
		self.fromPlace = from;
				
		NSMutableArray* toArray = [[NSMutableArray alloc] init];
		NSMutableArray* fromArray = [[NSMutableArray alloc] init];
		
		for (int i = 0; i < [toH length]; i=i+4)
			[toArray addObject:[toH substringWithRange:NSMakeRange(i, 4)]];
		
		for (int i = 0; i < [fromH length]; i=i+4)
			[fromArray addObject:[fromH substringWithRange:NSMakeRange(i, 4)]];
		
		self.toHours   = toArray;
		self.fromHours = fromArray;
	}
	
	return self;
}

//- (id)initfromJson:



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
	return NUM_OF_SECTIONS;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { 
	switch (section) {
		case kToSection:
			return toPlace;
		break;
		
		case kFromSection:
			return fromPlace;
		break;
		
		default:
			return @"LOLWUT?";
		break;
	}
}

- (NSInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger)section {
	switch (section) {
		case kToSection:
			return [toHours count];
		break;
		
		case kFromSection:
			return [fromHours count];
		break;
		
		default:
			return -1;
		break;
	}
}

- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }
	
	// Configure the cell
	[cell setUserInteractionEnabled:NO];
	switch (indexPath.section) {
		case kToSection:
			[cell setText:[toHours objectAtIndex:indexPath.row]];
		break;

		case kFromSection:
			[cell setText:[fromHours objectAtIndex:indexPath.row]];
		break;

		default:
			break;
	}
	

	return cell;
}	

/*
 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
 }
 */

/*
 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
 
 if (editingStyle == UITableViewCellEditingStyleDelete) {
 }
 if (editingStyle == UITableViewCellEditingStyleInsert) {
 }
 }
 */

/*
 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
 return YES;
 }
 */

/*
 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
 }
 */

/*
 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
 return YES;
 }
 */


@end
